package com.gaohe.controller;


import com.gaohe.common.R;
import com.gaohe.pojo.StadBuyCard;
import com.gaohe.pojo.StadCard;
import com.gaohe.pojo.StadMember;
import com.gaohe.pojo.SysUser;
import com.gaohe.service.StadMemberService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.Duration;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liu
 * @since 2025-04-12
 */
@RestController
@RequestMapping("/member")
public class StadMemberController {
    @Autowired
    private StadMemberService stadMemberService;
    @GetMapping("getVipList")
    public R getVipList(HttpServletRequest request){
        System.err.println(request.getSession().getAttribute("user"));
//        final Long user = Long.valueOf((String) request.getSession().getAttribute("user"));
        return R.success(stadMemberService.getVipList(1l));
    }
    @GetMapping("getVipListAll")
    public R getAll(){
        return R.success(stadMemberService.getVipList(1l).size());
    }
    @GetMapping("getVipToOnMonth")
    public R getVipToOnMonth(){
        final Date date = new Date();
        int month = date.getMonth() + 1;
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        List<StadMember> memberList = stadMemberService.getMemberList(1l);
//        System.err.println(memberList);
        String format = memberList.get(0).getCreateTime().format(formatter);
        final long count = memberList.stream()
                .filter(req -> Integer.valueOf(req.getCreateTime().format(formatter).split("")[6]) == month)
                .count();
        return R.success(count);
    }
//    查看余额不足的人
    @GetMapping("getNoMoneyCount")
    public R getNoMoneyCount(){
        List<StadMember> memberAndBuyCard = stadMemberService.getMemberAndBuyCard(1l);
        int size = memberAndBuyCard.size();
        for (StadMember stadMember : memberAndBuyCard) {
            List<StadBuyCard> stadBuyCard = stadMember.getStadBuyCard();
            long count = stadBuyCard.stream().filter(req -> req.getNum() > 0)
                    .filter(req -> req.getRemainingTime().isAfter(LocalDateTime.now())).count();
            if (count>0){
                size--;
            }
        }
        return R.success(size);
    }
//    查看30天没上课的总人数
    @GetMapping("getMemberAndRecord")
    public R getRecord(){
        int count=0;
        List<StadMember> memberAndRecord = stadMemberService.getMemberAndRecord(1l);
        for (StadMember stadMember : memberAndRecord) {
            final long kuangke = stadMember.getStadCourseRecords().stream()
                    .filter(req -> req.getStatus().equals("旷课"))
                    .count();
            if (kuangke>=30){
                count++;
            }
        }
//        System.err.println(memberAndRecord);
        return R.success(count);
    }
//    查看未续费
    @GetMapping("getXuFei")
    public R getContinueMoney(){
        System.err.println("xufei");
        List<StadMember> memberAndBuyCard = stadMemberService.getMemberAndBuyCard(1l);
        int size = memberAndBuyCard.size();
        for (StadMember stadMember : memberAndBuyCard) {
            final long count = stadMember.getStadBuyCard().stream()
                    .filter(req -> Duration
                            .between(LocalDateTime.now(),req.getRemainingTime() ).toDays() >= 30)
                    .count();
            System.err.println(count);
            if (count>0){
                size--;
            }
        }
        System.err.println(size);
        return R.success(size);
    }
//    查看会员购置的会员卡
    @GetMapping("getCardToMember/{id}")
    public R getCardToMember(@PathVariable("id")Long id){
        List<StadCard> memberAndCard = stadMemberService.getMemberAndCard(id);
        System.err.println(memberAndCard);
        return R.success(memberAndCard.get(0));
    }
    @GetMapping("searchVipList")
    public R searchVipList(@RequestParam String name){
        System.err.println(name);
        List<SysUser> stadMembers = stadMemberService.searchAllByName(1l, name);
        return R.success(stadMembers);
    }
}

